'use strict';

// todoPage controller
app.controller('changePassword', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    $scope.title = {
        addTitle:'修改个人密码'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };

    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }

    $scope.row = {
        "userId":$scope.app.userIdId,
        "old":'',
        "new":'',
        "new2":''
    };
    function checkItems(){
        if($scope.row.new!==$scope.row.new2){
            showMessage('warning','',"两次新密码输入不一致！");
            return false;
        }else if($scope.row.new.length<6){
            showMessage('warning','',"密码不可少于六个字符！");
            return false;
        }else if($scope.row.new===$scope.row.old){
            showMessage('warning','',"新密码与旧密码不可以一样！");
            return false;
        }
        return true;
    }

    function addDo(){
        toaster.postInfo('/a_yongan/single/change',$scope.row).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','',data.data.error);
                $scope.row = {
                    "userId":$scope.app.userIdId,
                    "old":'',
                    "new":'',
                    "new2":''
                };
            }else{
                showMessage('warning','',data.data.error);
            }
        });
    }
    $scope.message = '是否确定修改？';
    $scope.saveWarehouse = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            addDo();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };


}])
;